package 力扣.数学;

public class 最小差值I908 {
    public static void main(String[] args) {

    }

    public static int smallestRangeI(int[] nums, int k) {//寻找最大最小值，并取差值，再减去2k
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int len = nums.length;
        int sum = 0;

        for (int i = 0; i < len; i++) {
            sum += nums[i];
        }
        int min = nums[0];
        int max = nums[0];
        for (int i = 1; i < len; i++) {
            int t = nums[i];
            if (max < t) max = t;
            if (min > t) min = t;
        }
        int diff = max - min;
        if ( diff > (2 * k) ) {//差值大于2K
           return diff - (2 * k);
        }
        return  0;//小于2K，说明差值为零
    }
}
